package com.smart4s.app.core.positionservice.utils;


import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.smart4s.app.core.positionservice.Api.ApiService;
import com.smart4s.app.core.positionservice.url.MyUrls;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Android Studio.
 * User: 赵志鹏
 * Date: 2019/12/27
 * Time: 13:57
 */
public class NetUtil {

    private ApiService apiService;

    private NetUtil() {
        //创建日志拦截器
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        //初始化Ok请求
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //添加日志拦截器
                .addInterceptor(interceptor)
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();

        //初始化Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyUrls.BASEURL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //初始化Apiservice
        apiService = retrofit.create(ApiService.class);
    }

    //Post请求
    @SuppressLint("CheckResult")
    public void onPostInfo(String url, Map<String, Object> map, final Class cla, final NetCallBack netCallBack) {
        apiService.onPostInfo(url, map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(ResponseBody responseBody) throws Exception {
                        Gson gson = new Gson();

                        if (netCallBack != null) {
                            Object o = gson.fromJson(responseBody.string(), cla);
                            netCallBack.onSuccess(o);
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                });

    }


    private static class NetHolder {
        private static NetUtil netUtil = new NetUtil();
    }

    public static NetUtil getInstance() {
        return NetHolder.netUtil;
    }


    public interface NetCallBack<T> {
        void onSuccess(T t);

        void onError(String error);
    }

}
